package com.example.hongyu.avtt.ui.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.example.hongyu.avtt.listener.OnItemClickListener;

import java.util.List;

public abstract class BaseAdapter<T, E extends BaseViewHolder> extends RecyclerView.Adapter<E> {

  public Context mContext;
  public List<T> mList;
  public OnItemClickListener mOnItemClickListener;

  public BaseAdapter(Context context, List<T> list){
    mContext = context;
    mList = list;
  }

  public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.mOnItemClickListener = onItemClickListener;
  }

  /**
   * 初始化布局
   */
  public abstract int getItemView();

  /**
   * 获取ViewHolder
   * @param itemView
   * @return
   */
  protected abstract E getViewHolder(View itemView);

  @Override
  public E onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(getItemView(), parent, false);
    return getViewHolder(view);
  }

  @Override
  public void onBindViewHolder(E holder, int position) {
    if(mOnItemClickListener != null) {
      holder.setOnItemClickListener(mOnItemClickListener);
    }
  }

  @Override
  public int getItemCount() {
    return mList != null ? mList.size() : 0;
  }

}
